<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\User;
use Hash;

class LoginController extends Controller
{
    //
    public function login()
    {
    	return view('admin.login');
    }

    public function dologin(Request $request)
    {
    	//表单验证
    	// $this->validate($request,[
    	// 	'username'=>'required',
    	// 	'passwrod'=>'required'
    	// ],[
    	// 	'username.required'=>'用户名不能为空',
    	// 	'password.required'=>'密码不能为空'

    	// ]);
    	// dd($request->username);die;
    	//创建用户模型
    	$user = User::where('username',$request->username)->first();

    	// dd($user);die;

    	if(empty($user)){
    		return back()->with('info','用户名不存在');
    	}

    	if(Hash::check($request->password,$user->password)){
    		// dd(Hash::check($request->password,$user->password));die;
    		//写入session
    		$request->session()->put('id',$user->id);

    		return redirect('/admin');
    	}else{
    		return back()->with('info','用户名或者密码不正确');
    	}
    }

    public function logout(Request $request)
    {
    	//删除session
    	$request->session()->forget('id');
    	return redirect('/admin/login');
    }


    public function register()
    {
    	return view('home.user.register');
    }
}
